#include "StdAfx.h"
#include "TString.h"
#include <algorithm>

TString::TString(void)
{
}

TString::~TString(void)
{
}

TString::TString(const TString& node) : std::string(node.c_str())
{
}

TString::TString(const char* strValue) : std::string(strValue)
{

}

TString::TString(const char value)
{
    operator = (value); 
}


TString::SplitList TString::Split(TString strKeyWorld)
{
    SplitList list;

    TString::iterator iter = std::search(begin(), end(), strKeyWorld.begin(), strKeyWorld.end());
    while(iter != end())
    {
        std::string strNode(begin(), iter);
        list.push_back(strNode.c_str());

        erase(begin(),iter+strKeyWorld.length());
        iter = std::search(begin(), end(), strKeyWorld.begin(), strKeyWorld.end());
    }

    if (!empty())
    {
        list.push_back(c_str());
    }

    return list;
}


TString& TString::operator >> (uint32& value)
{
    value = static_cast<uint32>(_atoi64(c_str()));
    return *this;
}

TString& TString::operator = (const char* strValue)
{
    if (NULL == strValue)
    {
        empty();
        return *this;
    }

    std::string::operator =(strValue);
    return *this;
}

TString& TString::operator = (const char value)
{
    std::string& strTemp = static_cast<std::string& >(*this);
    strTemp.resize(1);
    strTemp[0] = value;
    return *this;
}

void TString::MakeLower()
{
    for(size_t i = 0; i != length(); ++i)
    {
        operator[](i) = tolower(at(i));
    }
}

uint32 TString::Replace(TString strSrc, TString strDst)
{
    uint32 uCount = 0;
    std::string::iterator iter = std::search(begin(), end(), strSrc.begin(), strSrc.end());
    while (iter != end())
    {
        std::string::iterator end_iter = iter + strSrc.size();
        replace(iter, end_iter, strDst);
        iter = std::search(begin(), end(), strSrc.begin(), strSrc.end());
        ++uCount;
    }
    return uCount;
}